package com.smart.system.domain;

import com.alibaba.fastjson2.JSON;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.*;
import com.smart.common.annotation.Excel;
import com.smart.common.core.domain.BaseEntity;

import java.util.List;

/**
 * 区域对象 area
 * 
 * @author smart
 * @date 2024-09-18
 */
@EqualsAndHashCode(callSuper = true)
@AllArgsConstructor
@NoArgsConstructor
@Data
@Builder
@TableName("area")
public class Area extends BaseEntity
{
    private static final long serialVersionUID = 1L;

    /** 主键标识 */
    @Schema(description ="${comment}")
    @TableField(value = "id",exist = true)
    @TableId(type = IdType.AUTO)
    private Integer id;

    @Schema(description ="区域名称")
    @TableField(value = "name",exist = true)
    private String name;

    /** 部门号 */
    @Excel(name = "部门号")
    @Schema(description ="部门号")
    @TableField(value = "dept_id",exist = true)
    private String deptId;

    /** 乐观锁 */
    @Excel(name = "乐观锁")
    @Schema(description ="乐观锁")
    @TableField(value = "revision",exist = true)
    private String revision;

    /** 是否删除;0正常1删除 */
    @Schema(description ="乐观锁")
    @TableField(value = "del_flag",exist = true)
    private String delFlag;

    /** 是否启用;0正常1冻结 */
    @Excel(name = "是否启用;0正常1冻结")
    @Schema(description ="是否启用;0正常1冻结")
    @TableField(value = "status",exist = true)
    private String status;

    @Schema(description = "坐标经纬度集合")
    @TableField(exist = false)
    private List<AreaLocation> areaLocations;

    /** 位置JSON */
    @Excel(name = "位置json")
    @Schema(description ="不传/位置json")
    @TableField(value = "json_string",exist = true)
    private String jsonString;

    public void setJsonString(String jsonString) {
        this.jsonString = jsonString;
        this.areaLocations = JSON.parseArray(jsonString, AreaLocation.class);
    }

    public void setAreaLocations(List<AreaLocation> areaLocations) {
        this.areaLocations = areaLocations;
        this.jsonString = JSON.toJSONString(areaLocations);
    }
    public List<AreaLocation> getAreaLocations(){
        return JSON.parseArray(jsonString, AreaLocation.class);
    }

    /**
     * 判断一个地址是否在区域内
     * @param longitude
     * @param latitude
     * @return
     */
    public boolean isContains(String longitude, String latitude) {
        AreaLocation areaLocation = new AreaLocation(null,longitude, latitude,null);
        return AreaLocation.isPointInPolygon(areaLocation, getAreaLocations());
    }
}
